<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="故障类型" prop="type" v-if="false">
        <el-select v-model="queryParams.type" placeholder="请选择故障类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_malfunction_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="故障上报用户id" prop="loginId" v-if="false">
        <el-input
          v-model="queryParams.loginId"
          placeholder="请输入故障上报用户id"
          clearable
          @keyup.enter.native="handleQuery" v-if="false"
        />
      </el-form-item>
      <el-form-item label="医院或科室对接人" prop="userId" v-if="false">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入医院或科室对接人"
          clearable
          @keyup.enter.native="handleQuery" v-if="false"
        />
      </el-form-item>


<el-form-item label="医院" prop="type" >
  <el-select v-model="queryParams.hospitalId" placeholder="请选择医院" clearable>
    <el-option v-for="item in hospitalList" :key="item.hospitalId" 
          :label="item.hospitalName" :value="item.hospitalId" />
  </el-select>
</el-form-item>

      <el-form-item label="故障标题" prop="title" v-if="false">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入故障标题"
          clearable
          @keyup.enter.native="handleQuery" v-if="false"
        />
      </el-form-item>
     

<el-form-item label="科室" prop="type">
  <el-select v-model="queryParams.deptId" placeholder="请输入科室" clearable>
    <el-option v-for="item in departmentList" :key="item.deptId" :label="item.departmentName"
      :value="item.deptId" />
  </el-select>
</el-form-item>

      <el-form-item label="状态" prop="statue" v-if="false">
        <el-select v-model="queryParams.statue" placeholder="请选择1.已备案、2.修复中、3.已修复、4.无法修复" clearable>
          <el-option
            v-for="dict in dict.type.sys_malfunction_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="故障备案时间" prop="time">
        <el-date-picker clearable
          v-model="queryParams.time"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择故障备案时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单id" prop="orderId" v-if="false">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter.native="handleQuery" v-if="false"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:malfunction:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:malfunction:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:malfunction:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:malfunction:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="malfunctionList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="故障信息id" align="center" prop="malfunctionId" />
      <el-table-column label="故障类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_malfunction_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="故障描述" align="center" prop="describes" />
      <el-table-column label="故障上报用户id" align="center" prop="loginId" />
      <el-table-column label="医院或科室对接人" align="center" prop="userId" />
      <el-table-column label="医院ID" align="center" prop="hospitalId" />
      <el-table-column label="故障标题" align="center" prop="title" />
      <el-table-column label="科室ID" align="center" prop="deptId" />
      <el-table-column label="床位ID" align="center" prop="bedId" />
      <el-table-column label="状态" align="center" prop="statue"> -->
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="故障信息id" align="center" prop="malfunctionId" v-if="false" />
        <el-table-column label="故障类型" align="center" prop="type" v-if="false" />
        <el-table-column label="故障描述" align="center" prop="describes" />
        <el-table-column label="故障上报用户id" align="center" prop="loginId" v-if="false" />
        <el-table-column label="医院或科室对接人" align="center" prop="userId"  v-if="false"/>    
        <el-table-column label="医院ID" align="center" prop="hospitalId" v-if="false" />   
        <el-table-column label="医院" align="center" prop="hospitalName" />      
        <el-table-column label="故障标题" align="center" prop="title" />  
        <el-table-column label="科室" align="center" prop="departmentName" />
        <el-table-column label="床位编号" align="center" prop="bedCode" />
    <el-table-column label="故障状态" align="center" prop="statue" v-if="false">




        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_malfunction_status" :value="scope.row.statue"/>
        </template>
      </el-table-column>
      <el-table-column label="故障备案时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="orderNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:malfunction:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:malfunction:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改故障信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="故障类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择故障类型">
            <el-option
              v-for="dict in dict.type.sys_malfunction_type"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="故障描述" prop="describes">
          <el-input v-model="form.describes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="故障上报用户id" prop="loginId" v-if="false">
          <el-input v-model="form.loginId" placeholder="请输入故障上报用户id" v-if="false" />
        </el-form-item>
        <el-form-item label="医院或科室对接人" prop="userId" v-if="false">
          <el-input v-model="form.userId" placeholder="请输入医院或科室对接人" v-if="false"/>
        </el-form-item>

        <el-form-item label="医院" prop="type">
          <el-select v-model="queryParams.hospitalId" placeholder="请选择医院" clearable>
            <el-option v-for="item in hospitalList" :key="item.hospitalId" :label="item.hospitalName"
              :value="item.hospitalId" />
          </el-select>
        </el-form-item>
        <el-form-item label="科室" prop="type">
          <el-select v-model="queryParams.deptId" placeholder="请输入科室" clearable>
            <el-option v-for="item in departmentList" :key="item.deptId" :label="item.departmentName" :value="item.deptId" />
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="医院" prop="hospitalName">
          <el-input v-model="form.hospitalName" placeholder="请输入医院" />
        </el-form-item> -->
        <el-form-item label="故障标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入故障标题" />
        </el-form-item>
        <!-- <el-form-item label="科室" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入科室" />
        </el-form-item> -->
        <el-form-item label="床位ID" prop="bedId" >
          <el-input v-model="form.bedId" placeholder="请输入床位ID"/>
        </el-form-item>

        <el-form-item label="状态" prop="statue" >
          <el-select v-model="form.statue" placeholder="请选择1.已备案、2.修复中、3.已修复、4.无法修复" >
            <el-option
              v-for="dict in dict.type.sys_malfunction_status"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="故障备案时间" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择故障备案时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单id" prop="orderId" v-if="false">
          <el-input v-model="form.orderId" placeholder="请输入订单id" v-if="false" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMalfunction, getMalfunction, delMalfunction, addMalfunction, updateMalfunction } from "@/api/admin/malfunction";
import { listHospital } from "@/api/admin/hospital";
import { listDepartment } from "@/api/admin/department";
import { listOrder } from "@/api/admin/order";
export default {
  name: "Malfunction",
  dicts: ['sys_malfunction_type', 'sys_malfunction_status'],
  data() {
    return {
      bedIdList:[],
      departmentList:[],
      hospitalList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 故障信息表格数据
      malfunctionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        describes: null,
        loginId: null,
        userId: null,
        hospitalId: null,
        title: null,
        deptId: null,
        bedId: null,
        statue: null,
        time: null,
        orderId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

    };
  },

  created() {

    this.getList();
  },
  methods: {

    /** 查询故障信息列表 */
    getList() {
      this.loading = true;
      listMalfunction(this.queryParams).then(response => {
        this.malfunctionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listHospital().then(response => {
        this.hospitalList = response.rows;
        // this.total = response.total;
        // this.loading = false;
      });
      listDepartment().then(response => {
        this.departmentList = response.rows;
      });
      listOrder().then(response =>{
        this.bedIdList = response.rows;
      })



    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        malfunctionId: null,
        type: null,
        describes: null,
        loginId: null,
        userId: null,
        hospitalId: null,
        title: null,
        deptId: null,
        bedId: null,
        statue: null,
        time: null,
        orderId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.malfunctionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加故障信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const malfunctionId = row.malfunctionId || this.ids
      getMalfunction(malfunctionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改故障信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.malfunctionId != null) {
            updateMalfunction(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMalfunction(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const malfunctionIds = row.malfunctionId || this.ids;
      this.$modal.confirm('是否确认删除故障信息编号为"' + malfunctionIds + '"的数据项？').then(function() {
        return delMalfunction(malfunctionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('admin/malfunction/export', {
        ...this.queryParams
      }, `malfunction_${new Date().getTime()}.xlsx`)
    },
  }
};



</script>
